#!/bin/bash

set -e
pushd $(cd $(dirname ${0})/..; pwd) > /dev/null

clean() {
    echo "bigdl clean"
    rm -rf ./third_party/source/bigdl
}

sync() {
    echo "bigdl sync"
    mkdir -p "./third_party/source/bigdl/spark/dl/src/main/resources/serialization"
    curl --silent --location --output "./third_party/source/bigdl/spark/dl/src/main/resources/serialization/bigdl.proto" "https://github.com/intel-analytics/BigDL/blob/master/spark/dl/src/main/resources/serialization/bigdl.proto?raw=true"
}

schema() {
    echo "bigdl schema"
    [[ $(grep -U $'\x0D' ./source/bigdl-proto.js) ]] && crlf=1
    node ./tools/protoc.js --root bigdl --out ./source/bigdl-proto.js ./third_party/source/bigdl/spark/dl/src/main/resources/serialization/bigdl.proto
    if [[ -n ${crlf} ]]; then
        unix2dos --quiet --newfile ./source/bigdl-proto.js ./source/bigdl-proto.js
    fi
}

while [ "$#" != 0 ]; do
    command="$1" && shift
    case "${command}" in
        "clean") clean;;
        "sync") sync;;
        "schema") schema;;
    esac
done
